import DList from "@/components/dlist";
export default {
  data() {
    return {
      type_val: 1,
      category_id: "",
      address: {},
      banner: [],
      activity: [],
      button: [],
      goods_category: [],
      goodsList: [],
      config: {},
      agent: null,
      loading: false,
      finished: false,
      page: 1,
      shop_name: ""
    };
  },
  activated() {
    this.initData();
    if(this.$route.query.adcode) {
      this.address= {
        'addressComponent' : {
          'adcode': this.$route.query.adcode
        },
        'title': '城市名'
      };
      this.getData();
    } else {
      this.initLocation();
    }
  },
  mounted() {},
  methods: {
    initLocation() {
      this.fun
        .getLocation()
        .then(res => {
          this.address = res;
          this.getData();
        })
        .catch(err => {
          console.log(err);
        });
    },
    initData() {
      this.goodsList = [];
      this.loading = false;
      this.finished = false;
      this.page = 1;
    },
    routerGo() {
      this.$store.commit("setReferer", window.location.href);
      this.$router.push(this.fun.getUrl("o2oLocation"));
    },
    splitArr(arr, num) {
      var res = [];
      for (var i = 0, len = arr.length; i < len; i += num) {
        res.push(arr.slice(i, i + num));
      }
      return res;
    },
    goSearchPage() {
      this.$router.push(this.fun.getUrl("expressSearch"));
    },
    clickType(value,fields) {
      if(this.loading) {
        return
      }
      this[fields] = value;
      this.initData();
      this.getGoodsList();
    },
    async getData() {
      let { data, result, msg } = await $http.post("plugin.supplier-local-life.frontend.index.index", {
        adcode: this.address.addressComponent.adcode
      });
      if (result) {
        this.banner = data.banner;
        this.activity = data.activity;
        this.button = this.splitArr(data.button, 10);
        this.goods_category = data.goods_category;
        this.category_id = data.goods_category[0].id;
        this.getGoodsList();
        this.agent = data.agent;
        this.shop_name = data.shop_name;
        this.config = data.config;
        document.title = data.shop_name;
      } else {
        this.$toast(msg);
      }
    },
    async getGoodsList() {
      if (this.loading === true || this.finished === true) {
        return;
      }
      this.loading = true;
      let url = this.type_val == 1 ? "plugin.supplier-local-life.frontend.index.get-goods-list" : "plugin.supplier-local-life.frontend.index.get-supplier-store-list";
      let form = this.type_val == 1 ? {
        adcode: this.address.addressComponent.adcode,
        category_id: this.category_id,
        page: this.page
      } : {
        adcode: this.address.addressComponent.adcode,
        page: this.page
      };
      let { data, result, msg } = await $http.post(url, form);
      if (result) {
        this.goodsList.push(...data.data);
        if (data.data.length === 0 || data.data.length < data.per_page || data.current_page === data.last_page) {
          this.finished = true;
        }
        this.page++;
        this.loading = false;
      } else {
        this.$toast(msg);
      }
    },
    onLoad() {
      this.getGoodsList();
    },
    goPage(web_link) {
      if(web_link) {
        window.location.href = web_link;
      }
    },
    // 跳转商品详情
    goGoodsDetail(id) {
      this.$router.push(this.fun.getUrl("goods", { id }));
    },
    // 进店
    goEnterShop(id) {
      this.$router.push(this.fun.getUrl("SupplierShop", { id }));
    }
  },
  components: { DList }
};
